package com.lanou.beautifulwardrobe.Activity_zhuti;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.Response;
import com.google.gson.Gson;
import com.lanou.beautifulwardrobe.R;
import com.lanou.beautifulwardrobe.Activity_zhuti.Adapter.ActivityRecyclerViewAdapter;
import com.lanou.beautifulwardrobe.activity.IntroFActivity;
import com.lanou.beautifulwardrobe.activity.IntroOActivity;
import com.lanou.beautifulwardrobe.activity.IntroTActivity;
import com.lanou.beautifulwardrobe.contants.Contants;
import com.lanou.beautifulwardrobe.contants.VolleySingleton;
import com.lanou.beautifulwardrobe.entify.IntroData;
import com.lanou.beautifulwardrobe.entify.IntroResult;
import com.lanou.beautifulwardrobe.entify.ProductActivity;
import com.lanou.beautifulwardrobe.entify.ProductResult;
import com.lanou.beautifulwardrobe.listener_interface.MyHotItemClickListener;

import java.io.Serializable;
import java.util.List;

/**
 * Created by dllo on 15/10/24.
 */
public class ActivityProductFragment extends Fragment {

    private RecyclerView activityRecyclerView;
    private ActivityRecyclerViewAdapter adapter;
    private GridLayoutManager activityGM;
    private VolleySingleton volleySingleton = VolleySingleton.getInstance();
    private String id;

    public ActivityProductFragment(String id) {
        this.id = id;
    }

    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.maintab_activity_item, null);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        activityRecyclerView = (RecyclerView) view.findViewById(R.id.activityRecyclerView);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();
    }

    private void initView() {
        adapter = new ActivityRecyclerViewAdapter();
        activityGM = new GridLayoutManager(getActivity(), 1);
        activityRecyclerView.setLayoutManager(activityGM);
        activityRecyclerView.setAdapter(adapter);
    }

    private void initData() {
        String url = Contants.ACTIVITY_PRODUCT_URL_PREFIX + id + Contants.ACTIVITY_PRODUCT_URL_SUFFIX;
        volleySingleton.getStringRequset(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                ProductResult productResult = gson.fromJson(response, ProductResult.class);
                if (productResult.getMsg().equals("ok")) {
                    List<ProductActivity> productActivitys = productResult.getData().getActivities();
                    adapter.addData(productActivitys);

                    adapter.setOnMyHotItemClickListener(new MyHotItemClickListener() {
                        @Override
                        public void onMyHotItemClickListener(String url, String title, final String activity_type, final String ids) {

                            String urls = Contants.INTRO_URL_PREFIX + ids + Contants.INTRO_URL_SUFFIX;
                            volleySingleton.getStringRequset(urls, new Response.Listener<String>() {
                                @Override
                                public void onResponse(String response) {


                                    String urls = Contants.INTRO_URL_PREFIX + ids + Contants.INTRO_URL_SUFFIX;

                                    Gson gson = new Gson();
                                    IntroResult introResult = gson.fromJson(response, IntroResult.class);
                                    IntroData introData = introResult.getData();
                                    String activityType = introData.getContent().getActivity_type();

                                    String productUrl = Contants.INTRO_URL_PRODUCT_PREFIX + ids + Contants.INTRO_URL_PRODUCT_SUFFIX;
                                    if (activity_type.equals("1")) {
                                        Intent intent = new Intent(getActivity(), IntroOActivity.class);
                                        intent.putExtra("data", (Serializable) introData);
                                        intent.putExtra("url", productUrl);
                                        getActivity().startActivity(intent);
                                    } else if (activity_type.equals("5")) {

                                        Intent intent = new Intent(getActivity(), IntroFActivity.class);
                                        intent.putExtra("data", (Serializable) introData);
                                        getActivity().startActivity(intent);
                                    } else if (activity_type.equals("3")) {
                                        Intent intent = new Intent(getActivity(), IntroTActivity.class);
                                        intent.putExtra("data", (Serializable) introData);
                                        intent.putExtra("ids", ids);
                                        getActivity().startActivity(intent);
                                    }
                                }
                            });
                        }
                    });
                }
            }
        });


    }
}
